package com.neborosoft.android.gui;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.neborosoft.android.gui.adapters.listview.ViewArrayAdapter;
import com.neborosoft.lib.handlers.Callback;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 01.01.13
 * Time: 23:43
 * To change this template use File | Settings | File Templates.
 */
public final class Gui {
    public static void invertViewVisibility(View view){
        if(view.getVisibility() == View.GONE){
            view.setVisibility(View.VISIBLE);
        }
        else {
            view.setVisibility(View.GONE);
        }
    }

    public static void setViewVisibility(View view, boolean visibility){
        int value = View.GONE;
        if(visibility){
            value = View.VISIBLE;
        }
        view.setVisibility(value);
    }

    public static void swapViewVisibilities(View a, View b){
        int aVisibility = a.getVisibility();
        int bVisibility = b.getVisibility();
        a.setVisibility(bVisibility);
        b.setVisibility(aVisibility);
    }

    /**
     * This method convets dp unit to equivalent device specific value in pixels.
     *
     * @param dp A value in dp(Device independent pixels) unit. Which we need to convert into pixels
     * @param context Context to get resources and device specific display metrics
     * @return A float value to represent Pixels equivalent to dp according to device
     */
    public static float convertDpToPixel(float dp,Context context){
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float px = dp * (metrics.densityDpi/160f);
        return px;
    }
    /**
     * This method converts device specific pixels to device independent pixels.
     *
     * @param px A value in px (pixels) unit. Which we need to convert into db
     * @param context Context to get resources and device specific display metrics
     * @return A float value to represent db equivalent to px value
     */
    public static float convertPixelsToDp(float px,Context context){
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float dp = px / (metrics.densityDpi / 160f);
        return dp;
    }

    public static void iterateVisibleItems(AbsListView listView, Callback<View> callback){
        int first = listView.getFirstVisiblePosition();
        int last = listView.getLastVisiblePosition();

        for(int i = first; i <= last; i++){
            View child = listView.getChildAt(i);
            callback.call(child);
        }
    }

    public static <T> void iterateVisibleItems(AbsListView listView, List<T> elements, Callback<T> callback){
        int first = listView.getFirstVisiblePosition();
        int last = listView.getLastVisiblePosition();

        for(int i = first; i <= last; i++){
            T object = elements.get(i);
            callback.call(object);
        }
    }

    public static boolean containsView(View view, ViewGroup viewGroup){
        for(int i = 0; i < viewGroup.getChildCount(); i++){
            View child = viewGroup.getChildAt(i);
            if(child == view){
                return true;
            }
        }

        return false;
    }

    //ugly fix for api level < 11
    public static void setAdapter(AbsListView listView, ListAdapter adapter){
        if(listView instanceof GridView){
            ((GridView)listView).setAdapter(adapter);
        } else if(listView instanceof ListView) {
            ((ListView)listView).setAdapter(adapter);
        } else {
            listView.setAdapter(adapter);
        }
    }
}
